package com.burry.services;

import java.util.List;
import java.util.Map;

import com.burry.db.SQLMachine;
import com.burry.util.MapList;

public class ManageMenuService {
	
	/**
	 * 获取菜单列表
	 * @return
	 */
	public MapList getMenuList(){
		
		SQLMachine sqlM = new SQLMachine("default");
		List menuList = sqlM.getResultSet("select * from bu_menu");
		
		if(menuList == null)
			return null;
		MapList ml = new MapList();
		//上级菜单编码为 ml 的key
		for(int i=0;i<menuList.size();i++){
			Map tmp = (Map)menuList.get(i);
			String upper = (String)tmp.get("upper_menu");
			ml.put(upper, tmp);
		}
		return ml;
	}
	
	public String getMenuStrFromML(MapList ml){
		StringBuffer sb = new StringBuffer();
		List topFather = (List)ml.get("-1");
		for(int i=0;i<topFather.size();i++){
			Map tmp = (Map)topFather.get(i);
			String menuCode = (String)tmp.get("menu_code");
			String menuName = (String)tmp.get("menu_name");
			String requestUri = (String)tmp.get("request_uri");
			sb.append("<li><a href='#' onclick='turnPage(\"" +requestUri+ "\")'>" + menuName + "</a>");
			List subList = (List)ml.get(menuCode);
			if(subList != null){
				sb.append("<ul>");
				for(int j=0;j<subList.size();j++){
					Map t = (Map)subList.get(j);
					//String mCode = (String)t.get("menu_code");
					String mName = (String)t.get("menu_name");
					String rUri = (String)t.get("request_uri");
					sb.append("<li><a href='#' onclick='turnPage(\"" +rUri+ "\")'>" + mName + "</a>");
				}
				sb.append("</ul>");
			}
			
			sb.append("</li>");
		}
		return sb.toString();
	}
}
